/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.account.ZAttrProvisioning.DomainType;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.IDNUtil;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.NamedEntry.Visitor;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.ldap.LdapHelper;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.ldap.IAttributes;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory.FilterId;
/*     */ import com.zimbra.cs.ldap.ZSearchScope;
/*     */ import com.zimbra.qa.unittest.TestUtil;
/*     */ import com.zimbra.qa.unittest.prov.Names;
/*     */ import com.zimbra.soap.admin.type.CacheEntryType;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLdapProvDomain
/*     */   extends LdapTest
/*     */ {
/*     */   private static LdapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  59 */     provUtil = new LdapProvTestUtil();
/*  60 */     prov = provUtil.getProv();
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  65 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */   private static String makeTestDomainName(String prefix) {
/*  69 */     String baseDomainName = baseDomainName();
/*     */     String domainName;
/*  71 */     String domainName; if (prefix == null) {
/*  72 */       domainName = baseDomainName;
/*     */     } else {
/*  74 */       domainName = prefix.toLowerCase() + "." + baseDomainName;
/*     */     }
/*     */     
/*  77 */     return Names.makeDomainName(domainName);
/*     */   }
/*     */   
/*     */   private Domain createDomain(String domainName) throws Exception {
/*  81 */     return createDomain(domainName, null);
/*     */   }
/*     */   
/*     */   private Domain createDomain(String domainName, Map<String, Object> attrs) throws Exception
/*     */   {
/*  86 */     return provUtil.createDomain(domainName, attrs);
/*     */   }
/*     */   
/*     */   private void deleteDomain(Domain domain) throws Exception {
/*  90 */     provUtil.deleteDomain(domain);
/*     */   }
/*     */   
/*     */ 
/*     */   private void verifyAllDomains(List<Domain> allDomains)
/*     */     throws Exception
/*     */   {
/*  97 */     Set<String> expectedDomains = new HashSet();
/*  98 */     String defaultDomainName = Provisioning.getInstance().getConfig().getDefaultDomainName();
/*     */     
/* 100 */     expectedDomains.add(defaultDomainName);
/* 101 */     expectedDomains.add("example.com");
/*     */     
/* 103 */     Assert.assertEquals(expectedDomains.size(), allDomains.size());
/*     */     
/* 105 */     for (Domain domain : allDomains) {
/* 106 */       Assert.assertTrue(expectedDomains.contains(domain.getName()));
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 112 */     LdapHelper ldapHelper = ((LdapProv)prov).getHelper();
/* 113 */     final List<String> domainIds = new ArrayList();
/* 114 */     SearchLdapOptions.SearchLdapVisitor visitor = new SearchLdapOptions.SearchLdapVisitor()
/*     */     {
/*     */       public void visit(String dn, Map<String, Object> attrs, IAttributes ldapAttrs) {
/*     */         try {
/* 118 */           domainIds.add(ldapAttrs.getAttrString("zimbraId"));
/*     */         } catch (ServiceException e) {
/* 120 */           Assert.fail();
/*     */         }
/*     */         
/*     */       }
/* 124 */     };
/* 125 */     SearchLdapOptions searchOpts = new SearchLdapOptions("", ZLdapFilterFactory.getInstance().fromFilterString(ZLdapFilterFactory.FilterId.UNITTEST, "(objectclass=zimbraDomain)"), new String[] { "zimbraId" }, 0, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 131 */     ZLdapContext zlc = null;
/*     */     try {
/* 133 */       zlc = LdapClient.getContext(LdapUsage.UNITTEST);
/* 134 */       ldapHelper.searchLdap(zlc, searchOpts);
/*     */     } finally {
/* 136 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */     
/* 139 */     Assert.assertEquals(domainIds.size(), allDomains.size());
/*     */     
/* 141 */     for (Domain domain : allDomains) {
/* 142 */       Assert.assertTrue(domainIds.contains(domain.getId()));
/*     */     }
/*     */   }
/*     */   
/*     */   private void getDomainById(String id) throws Exception {
/* 147 */     prov.flushCache(CacheEntryType.domain, null);
/* 148 */     Domain domain = prov.get(Key.DomainBy.id, id);
/* 149 */     Assert.assertEquals(id, domain.getId());
/*     */   }
/*     */   
/*     */   private void getDomainByName(String name) throws Exception {
/* 153 */     prov.flushCache(CacheEntryType.domain, null);
/* 154 */     Domain domain = prov.get(Key.DomainBy.name, name);
/* 155 */     Assert.assertEquals(IDNUtil.toAsciiDomainName(name), domain.getName());
/*     */   }
/*     */   
/*     */   private void getDomainByVirtualHostname(String virtualHostname, String expectedDomainId) throws Exception
/*     */   {
/* 160 */     prov.flushCache(CacheEntryType.domain, null);
/* 161 */     Domain domain = prov.get(Key.DomainBy.virtualHostname, virtualHostname);
/* 162 */     Assert.assertEquals(expectedDomainId, domain.getId());
/*     */   }
/*     */   
/*     */   private void getDomainByKrb5Realm(String krb5Realm, String expectedDomainId) throws Exception {
/* 166 */     prov.flushCache(CacheEntryType.domain, null);
/* 167 */     Domain domain = prov.get(Key.DomainBy.krb5Realm, krb5Realm);
/* 168 */     Assert.assertEquals(expectedDomainId, domain.getId());
/*     */   }
/*     */   
/*     */   private void getDomainByForeignName(String foreignName, String expectedDomainId) throws Exception {
/* 172 */     prov.flushCache(CacheEntryType.domain, null);
/* 173 */     Domain domain = prov.get(Key.DomainBy.foreignName, foreignName);
/* 174 */     Assert.assertEquals(expectedDomainId, domain.getId());
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void createTopDomain() throws Exception {
/* 179 */     String DOMAIN_NAME = makeTestDomainName(null);
/* 180 */     Domain domain = createDomain(DOMAIN_NAME);
/*     */     
/* 182 */     deleteDomain(domain);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void createSubDomain() throws Exception {
/* 187 */     String DOMAIN_NAME = makeTestDomainName(genDomainSegmentName() + ".sub1.sub2");
/* 188 */     Domain domain = createDomain(DOMAIN_NAME);
/*     */     
/* 190 */     deleteDomain(domain);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void createDomainAlreadyExists() throws Exception {
/* 195 */     String DOMAIN_NAME = makeTestDomainName(genDomainSegmentName());
/* 196 */     Domain domain = createDomain(DOMAIN_NAME);
/*     */     
/* 198 */     boolean caughtException = false;
/*     */     try {
/* 200 */       prov.createDomain(DOMAIN_NAME, new HashMap());
/*     */     } catch (AccountServiceException e) {
/* 202 */       if ("account.DOMAIN_EXISTS".equals(e.getCode())) {
/* 203 */         caughtException = true;
/*     */       }
/*     */     }
/* 206 */     Assert.assertTrue(caughtException);
/*     */     
/* 208 */     deleteDomain(domain);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void deleteNonEmptyDomain() throws Exception {
/* 213 */     String DOMAIN_NAME = makeTestDomainName(genDomainSegmentName());
/* 214 */     Domain domain = createDomain(DOMAIN_NAME);
/*     */     
/* 216 */     String ACCT_NAME = TestUtil.getAddress("acct", DOMAIN_NAME);
/* 217 */     Account acct = prov.createAccount(ACCT_NAME, "test123", null);
/*     */     
/* 219 */     boolean caughtException = false;
/*     */     try {
/* 221 */       prov.deleteDomain(domain.getId());
/*     */     } catch (ServiceException e) {
/* 223 */       if ("account.DOMAIN_NOT_EMPTY".equals(e.getCode())) {
/* 224 */         caughtException = true;
/*     */       }
/*     */     }
/* 227 */     Assert.assertTrue(caughtException);
/*     */     
/*     */ 
/* 230 */     prov.deleteAccount(acct.getId());
/* 231 */     deleteDomain(domain);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void getAllDomain() throws Exception {
/* 236 */     List<Domain> allDomains = prov.getAllDomains();
/* 237 */     verifyAllDomains(allDomains);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void getAllDomainVisitor() throws Exception {
/* 242 */     final List<Domain> allDomains = new ArrayList();
/*     */     
/* 244 */     NamedEntry.Visitor visitor = new NamedEntry.Visitor()
/*     */     {
/*     */       public void visit(NamedEntry entry) throws ServiceException {
/* 247 */         allDomains.add((Domain)entry);
/*     */       }
/*     */       
/* 250 */     };
/* 251 */     prov.getAllDomains(visitor, new String[] { "zimbraId" });
/*     */     
/* 253 */     verifyAllDomains(allDomains);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testAliasDomain() throws Exception {
/* 258 */     String TARGET_DOMAIN_NAME = makeTestDomainName(genDomainSegmentName("target"));
/* 259 */     String ALIAS_DOMAIN_NAME = makeTestDomainName(genDomainSegmentName("alias"));
/* 260 */     String USER_LOCAL_PART = "user";
/*     */     
/* 262 */     Domain targetDomain = prov.get(Key.DomainBy.name, TARGET_DOMAIN_NAME);
/* 263 */     Assert.assertNull(targetDomain);
/* 264 */     Domain aliasDomain = prov.get(Key.DomainBy.name, ALIAS_DOMAIN_NAME);
/* 265 */     Assert.assertNull(aliasDomain);
/*     */     
/* 267 */     targetDomain = createDomain(TARGET_DOMAIN_NAME);
/*     */     
/* 269 */     Map<String, Object> attrs = new HashMap();
/* 270 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.alias.name());
/* 271 */     attrs.put("zimbraDomainAliasTargetId", targetDomain.getId());
/* 272 */     aliasDomain = createDomain(ALIAS_DOMAIN_NAME, attrs);
/*     */     
/* 274 */     String realEmail = prov.getEmailAddrByDomainAlias(TestUtil.getAddress(USER_LOCAL_PART, ALIAS_DOMAIN_NAME));
/* 275 */     Assert.assertEquals(IDNUtil.toAscii(TestUtil.getAddress(USER_LOCAL_PART, TARGET_DOMAIN_NAME)), realEmail);
/*     */     
/*     */ 
/* 278 */     deleteDomain(aliasDomain);
/* 279 */     deleteDomain(targetDomain);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void getEmailAddrByDomainAlias() throws Exception
/*     */   {}
/*     */   
/*     */   @Test
/*     */   public void getDomain() throws Exception
/*     */   {
/* 289 */     String DOMAIN_NAME = makeTestDomainName(genDomainSegmentName());
/*     */     
/* 291 */     String VIRTUAL_HOSTNAME = "virtual.com";
/* 292 */     String KRB5_REALM = "KRB5.REALM";
/* 293 */     String FOREIGN_NAME = "foreignname";
/*     */     
/* 295 */     Map<String, Object> attrs = new HashMap();
/* 296 */     attrs.put("zimbraVirtualHostname", VIRTUAL_HOSTNAME);
/* 297 */     attrs.put("zimbraAuthKerberos5Realm", KRB5_REALM);
/* 298 */     attrs.put("zimbraForeignName", FOREIGN_NAME);
/*     */     
/* 300 */     Domain domain = createDomain(DOMAIN_NAME, attrs);
/*     */     
/* 302 */     String domainId = domain.getId();
/*     */     
/* 304 */     getDomainById(domainId);
/* 305 */     getDomainByName(DOMAIN_NAME);
/* 306 */     getDomainByVirtualHostname(VIRTUAL_HOSTNAME, domainId);
/* 307 */     getDomainByKrb5Realm(KRB5_REALM, domainId);
/* 308 */     getDomainByForeignName(FOREIGN_NAME, domainId);
/*     */     
/* 310 */     deleteDomain(domain);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void getDomainNotExist() throws Exception {
/* 315 */     String DOMAIN_NAME = makeTestDomainName(genDomainSegmentName());
/* 316 */     Domain domain = prov.get(Key.DomainBy.name, DOMAIN_NAME);
/* 317 */     Assert.assertNull(domain);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestLdapProvDomain.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */